package com.bsh.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsh.mybatis.model.IndexImage;
import com.bsh.repository.DaoIndexImage;
import com.bsh.service.IndexImageService;
import com.bsh.utils.Pager;
import com.bsh.utils.SimpleCache;
import com.mysql.jdbc.StringUtils;

/**
 * 图片
 * @author gsh
 *
 */
@Service
@Transactional
public class IndexImageServiceImpl implements IndexImageService {
	public static SimpleCache<Integer>cache=new SimpleCache<Integer>(1800);
	@Autowired
	private DaoIndexImage indxImgDao;
	

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.IndexImageService#getPage(com.bsh.utils.Pager, java.lang.String, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> getPage(Pager pager,String title) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isNullOrEmpty(title))
			sb.append(String.format(" and u.title = '%s' ",title.trim()));	
		sb.append(" and type='index' ");
		List<Map<String, Object>> list = indxImgDao.getPage(pager,sb.toString());
		return list;
	}
	
	@Override
	public List<Map<String, Object>> getMerchantPage(Pager pager,String title) {
		StringBuilder sb =new StringBuilder();
		if(!StringUtils.isNullOrEmpty(title))
			sb.append(String.format(" and u.title = '%s' ",title.trim()));
		sb.append(" and type='merchant' ");
		List<Map<String, Object>> list = indxImgDao.getPage(pager,sb.toString());
		return list;
	}	
	
	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.IndexImageService#getIndexImage(java.lang.Integer)
	 */
	@Override
	public IndexImage getIndexImage(Integer id) {
		return indxImgDao.selectByPrimaryKey(id);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.IndexImageService#addIndexImage(com.bsh.mybatis.model.IndexImage)
	 */
	@Override
	public Integer addIndexImage(IndexImage image) {
		return indxImgDao.insert(image);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.IndexImageService#editIndexImage(com.bsh.mybatis.model.IndexImage, boolean)
	 */
	@Override
	public Integer editIndexImage(IndexImage image,boolean updateImage) {
		IndexImage imgOld = indxImgDao.selectByPrimaryKey(image.getId());
		image.setType(imgOld.getType());
		if(updateImage)
		  return indxImgDao.updateByPrimaryKey(image);
		else{
			return indxImgDao.updateByPrimaryKeyExclueImg(image);
		}
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.IndexImageService#enable(int, boolean)
	 */
	@Override
	public Integer enable(int id, boolean isEnabele) {
		return indxImgDao.enable(id, isEnabele ? 1 : 0);
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.IndexImageService#disableBatch(int[])
	 */
	@Override
	public Integer disableBatch(int[] idList) {
		if (idList != null && idList.length == 0)
			return 0;
		for (int id : idList) {
			indxImgDao.enable(id, 0);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.IndexImageService#enableBatch(int[])
	 */
	@Override
	public Integer enableBatch(int[] idList) {
		if (idList != null && idList.length == 0)
			return 0;
		for (int id : idList) {
			indxImgDao.enable(id, 1);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.bsh.serviceImpl.IndexImageService#deleteIndexImage(int)
	 */
	@Override
	public Integer deleteIndexImage(int id) {
		return indxImgDao.deleteByPrimaryKey(id);
	}

	@Override
	public List<Map<String, Object>> getMyPage(String page) {
		return indxImgDao.getMyPage(page);
	}

	@Override
	public void updateMyPage(String page) throws Exception{
	   	indxImgDao.update(page);
	}

	@Override
	public void updateMyPageBatch(JSONArray list) throws Exception {
		String[] pages = new String[list.size()];
		JSONObject sql=new JSONObject();
		for(int i=0;i<list.size();i++){
			sql= (JSONObject) list.get(i);
			pages[i]=sql.getString("page");
		}
		indxImgDao.updateBatch(pages);
	}

	@Override
	public String getUserPage(String page) {
		return indxImgDao.getUserPage(page);
	}

	@Override
	public String getUser(String page) {
		return indxImgDao.getUser(page);
	}

	@Override
	public String updatePage(String page){
		return indxImgDao.updateUserPage(page);
	}

	@Override
	public String insertPage(String page){
		return indxImgDao.insertUserPage(page);
	}

	@Override
	public String deletePage(String page){
		return indxImgDao.deleteUserPage(page);
	}

	
	
	
}
